
var ergouMySql = require('./mysqlUtil')
var axios = require('axios');

const ergou = axios.create({
    baseURL: 'https://app.ojbk66plus.top'
  });

var domain = 'https://app.ojbk66plus.top';

var cdkey = '40a642a0'
var wxid = 'wxid_xav3iskt0zbm22'
var savedData = {"type":"object","data":["女",[18,30],[150,200],[{"name":"大专","checked":true,"disabled":false},{"name":"本科","checked":true,"disabled":false},{"name":"硕士","checked":true,"disabled":false},{"name":"博士","checked":true,"disabled":false}],"重庆-重庆","不限-不限"]}

var total=0;
function main(){
    var pageNo = 1;
    var isLoop = true;
    do{
      getUserList(pageNo)
      pageNo++;
      isLoop = false;
    }while(pageNo < 114)
    console.log("查询完成")
}

async function getUserList(pageNo){
  console.log("开始查询数据，当前准备查询第"+pageNo+"页的数据")
  var searchUrl = '/api/search'
  var requestData={"gender":"女","birthYear":{"start":18,"end":30},"height":{"start":150,"end":200},"highestdegree":["大专","本科","硕士","博士"],"lifecity":"重庆-重庆","homecity":"不限-不限","page":pageNo,"cdKey":cdkey};

  ergou({
      method: 'post',
      url: searchUrl,
      http2: true, // 启用http2协议
      data: requestData,
      synchronous: true,
    }).then(function (response) {
      // 请求成功后的处理
      console.log(response)
       var userList = response.data;
       if (Array.isArray(userList) && userList.length === 0) {
        console.log("未查询到数据,循环结束")
        isLoop = false;
       }else {
        for(user of userList){
          getUserInfo(user);
         }
       }
    })
    .catch(function (error) {
      // 请求失败后的处理
      console.log("查询列表异常:",pageNo,error);
      getUserList(pageNo);
    });
}

async function getUserInfo(user){
    await sleep(1500)
    console.log("准备获取用户详情数据，userId", user.uid)
    var url =  '/api/userInfo?uid='+user.uid+'&cdKey='+cdkey;
    ergou({
        method: 'get',
        url: url,
        http2: true, // 启用http2协议
        synchronous: true,
      }).then(function (response) {
        // 请求成功后的处理
        var data = response.data;
        var cardList = data.cardList.split("cardList")
        data.selfIntroduction = cardList[1];
        data.family = cardList[3]
        data.hobby = cardList[5]
        data.viewOfLove = cardList[7]
        data.otherHalf = cardList[9]
        data.reason = cardList[11]
        data.life = cardList[13]
        delete data.cardList
        delete data.state
        console.log("准备保存用户：uid",  user.uid)
        ergouMySql.connection.query('INSERT INTO ergou_user SET ?', data, (error, result) => {
          if (error) {
            console.error('Error executing query:', error);
            return;
          }
          console.log("保存用户：uid",  user.uid, "成功")
        });
      })
      .catch(function (error) {
        // 请求失败后的处理
        console.log("查询详情异常:uid:",user.uid,"准备重试");
        getUserInfo(user);
      });
}

function sleep(ms) {
  return new Promise(resolve=>setTimeout(resolve, ms))
}

// var user = {uid:"22125810223"}
// var data = getUserInfo(user);
// console.log(data)
function test(){
  var user = {uid:23117015002}
  getUserList(113);
}
//test();

main();